<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<x:parse xml="${param.xmlstr}" var="xmlData" />
<div class="t_bar"><span class="color_red">*</span>任务分工及经费支出情况（协作单位自由添加）</div>
<table  id="tblOrgOrgs" cellpadding="0" class="table01 t_css" width="100%" border="0" cellpadding="0" style="*border-bottom: #a1c0dc 1px solid;">
			<thead >
					<tr style="border:1px solid #B7D1E6">
						<th class="TblHead" align="center"  width="6%" rowspan="2">选择</th>
						<th class="TblHead" align="center"  width="6%" rowspan="2">序号</th>
						<th class="TblHead" align="center"  width="18%" rowspan="2">单位名称</th> 
						<th class="TblHead" align="center"  width="46%" rowspan="2">任务分工（限150字）</th> 
				      <th class="TblHead" align="center"  width="24%" colspan="2">实施费用（万元）</th> 
					</tr>
					<tr>
						<th class="TblHead" align="center"  width="12%">计划新增投资</th>
						<th class="TblHead" align="center"  width="12%">其中：专项经费</th>
					</tr>
			</thead>
			
			<tbody id="tab">
					
					<tr style="display:none" align="center">
						<td align="center">
							<input type="radio" class="radiobutton" name="selOrgOrgs" >
						</td>
						<td align="center">
							<span name="orgs|org[0]@seq_no" >[index]</span> 
						</td>
						<td align="center">
							<input type="text" name="orgs|org[0]|name" label="单位名称" class=" required input_text maxlength:50" maxlength="50" style="width:80%"/>
						</td>
						
						<td align="center">
							<input type="text" name="orgs|org[0]|duty" label="任务分工" class=" required input_text maxlength:150" maxlength="150" style="width:90%"/>
						</td>	
						
						<td align="center">
							<input type="text" id="add_[index]" name="orgs|org[0]|add" label="计划新增投资"  class="input_text required valueCompare:fund_[index]:add_[index]"  onfocus="InitInput.setNumber(this,7,4,2)" onchange="sumTotal('tblOrgOrgs','add','add_total');"  style="width:90%;text-align: right;" />
						</td> 
					
						<td align="center">
							<input type="text" id="fund_[index]" name="orgs|org[0]|fund" label="其中：专项经费" onfocus="InitInput.setNumber(this,7,4,2)" onchange="sumTotal('tblOrgOrgs','fund','fund_total');" class=" input_text required" style="width:90%;text-align: right;"/>
						</td> 
					</tr>
					
					<!-- 申请单位 -->
					<c:set value="0" var="index" scope="page"/>
					<c:set value="0" var="flag" scope="page"/>
					<tr align="center">
						<td align="center"> 
							<input type="radio" class="radiobutton firstRadio" name="selOrgOrgs" >
						</td>
						<td align="center"> 
							<span name="orgs/org[${flag}${index}]@seq_no" >1</span> 
						</td>
						<td align="center">
							<span type="text" name="orgs/org[${flag}${index}]/name" >
								<x:out select="$xmlData/data/organizations/organization[@submit_org='1']/org_name" />
							</span>
						</td>
						
						<td align="center">
							<input type="text" name="orgs/org[${flag}${index}]/duty" label="任务分工" class=" required input_text maxlength:150" maxlength="150" style="width:90%" value="<x:out select="$xmlData/data/orgs/org[@seq_no='1']/duty"/>"/>
						</td>	
						
						<td align="center">
							<input type="text" id="add_${index}" name="orgs/org[${flag}${index}]/add" value="<x:out select="$xmlData/data/orgs/org[@seq_no='1']/add"/>" label="计划新增投资"  class="input_text required valueCompare:fund_${index }:add_${index }" onchange="sumTotal('tblOrgOrgs','add','add_total');"  onfocus="InitInput.setNumber(this,7,4,2)" style="width:90%;text-align: right;" />
						</td> 
					
						<td align="center">
							<input type="text" id="fund_${index}" name="orgs/org[${flag}${index}]/fund" value="<x:out select="$xmlData/data/orgs/org[@seq_no='1']/fund"/>" label="其中：专项经费" onfocus="InitInput.setNumber(this,7,4,2)" onchange="sumTotal('tblOrgOrgs','fund','fund_total');" class=" input_text required" style="width:90%;text-align: right;"/>
						</td> 
					</tr>
					<c:set value="${index+1}" var="index" scope="page"/>
					
					<!-- 协作单位1 -->
				<%-- 	<c:set value="0" var="index" scope="page"/> --%>
					<x:forEach select="$xmlData/data/orgs/org" var="org" begin="1">
					<c:choose>
						<c:when test="${index<10}" ><c:set value="0" var="flag" scope="page"/></c:when>
						<c:otherwise><c:set value="" var="flag" scope="page"/></c:otherwise>
					</c:choose>
					<tr align="center">
						<td align="center"> 
							<input type="radio" class="radiobutton" name="selOrgOrgs" >
						</td>
						<td align="center"> 
							<span name="orgs/org[${flag}${index}]@seq_no" ><x:out select="$org/@seq_no"/></span> 
						</td>
						<td align="center">
							<input type="text" name="orgs/org[${flag}${index}]/name" label="单位名称" class=" required input_text maxlength:50" maxlength="50" style="width:80%" value="<x:out select="$org/name"/>"/>
						</td>
						
						<td align="center">
							<input type="text" name="orgs/org[${flag}${index}]/duty" label="任务分工" class=" required input_text maxlength:150" maxlength="150" style="width:90%" value="<x:out select="$org/duty"/>"/>
						</td>	
						
						<td align="center">
							<input type="text" id="add_${index}" name="orgs/org[${flag}${index}]/add" value="<x:out select="$org/add"/>" label="计划新增投资"  class="input_text required valueCompare:fund_${index }:add_${index }" onchange="sumTotal('tblOrgOrgs','add','add_total');"  onfocus="InitInput.setNumber(this,7,4,2)" style="width:90%;text-align: right;" />
						</td> 
					
						<td align="center">
							<input type="text" id="fund_${index}" name="orgs/org[${flag}${index}]/fund" value="<x:out select="$org/fund"/>" label="其中：专项经费" onfocus="InitInput.setNumber(this,7,4,2)" onchange="sumTotal('tblOrgOrgs','fund','fund_total');" class=" input_text required" style="width:90%;text-align: right;"/>
						</td> 
						
					</tr>
					<c:set value="${index+1}" var="index" scope="page"/>
					</x:forEach>
				</tbody>
			</table>
			
		<table cellpadding="0" class="table01 t_css" width="100%" border="0" cellpadding="0" style="*border-bottom: #a1c0dc 1px solid;" id="countOrgsTotal">
				<tr>
					<td align="center" width="76%">经费预算合计</td>
					<td width="12%" align="center">
						<input  id="add_total" style="width:90%;text-align: right;"  disabled="disabled " class="input_text gray valueEqual:add_total:source_add" label="经费预算合计-->计划新增投资合计" name="orgs/add_total" value='<x:out select="$xmlData/data/orgs/add_total"/>'>
					</td>
					<td width="12%" align="center"> 
						 <input  id="fund_total" style="width:90%;text-align: right;"  disabled="disabled" class=" input_text gray valueEqual:fund_total:source_apply" label="经费预算合计-->其中：专项经费合计" name="orgs/fund_total" value='<x:out select="$xmlData/data/orgs/fund_total"/>'>
					</td>
				</tr>
		</table>
				</tr>
			<table width="98%" align="center" border="0">
					<tr style="border: 0"><td style="border: 0">
					<input class="button" name="cmdAddbefore" type=button value="添加" onclick="addNewRow('tblOrgOrgs');bindClickForRadio();try{top.resizeFrame();}catch(e){};">
					<input class="button" id="cmdMovePrev" name="cmdMovePrev" type=button value="上移" onclick="movePrev('tblOrgOrgs','selOrgOrgs')">
					<input class="button" id="cmdMoveNext" name="cmdMoveNext" type=button value="下移" onclick="moveNext('tblOrgOrgs','selOrgOrgs')">
					<input class="button" id="cmddel" name="cmddel" type=button  value="删除" onclick="deleteSelectedRow('tblOrgOrgs','条数据');sumTotal('tblOrgOrgs','add','add_total');sumTotal('tblOrgOrgs','fund','fund_total');">
					</td>
			</table>
		<p style="font-size: 13px;">注：此类项目原则上由市县科技局牵头实施，申请单位为市县（区）科技局。</p>
	<script type="text/javascript">
	
		// 第一行不能 上/下移 删除 操作
		$(document).ready(function(){
			bindClickForRadio();
		});
		
		// 第一行不能 上/下移 删除 操作
		function bindClickForRadio(){
			var classString = '';
			$("#tblOrgOrgs").find(":radio:gt(0)").unbind("click").click(function(){   //排除隐藏行
			 	classString = $(this).attr("class");		
				if (classString.indexOf("firstRadio") > -1)
				{
					$("#cmdMovePrev,#cmdMoveNext,#cmddel").attr("disabled",true);
				}
				else
				{
					$("#cmdMovePrev,#cmdMoveNext,#cmddel").attr("disabled",false);
				}
			});
		}
	
	 //对id="tableId"某表中，后缀名为sourceInputName所有求和，赋值给id="destInputId"的input
	function sumTotal(tableId,sourceInputName,destInputId){
		var sourceInputs = $("#"+tableId).find("input[name$='"+sourceInputName+"']");
		var count = 0;
		for ( var i = 0; i < sourceInputs.length; i++) {
		var fund = $(sourceInputs[i]).val();
		if (!$.isNumeric(fund))
		fund = "0";
		count = ((parseFloat(count)*10000 + parseFloat(fund)*10000)/10000).toFixed(4);
		}
		count = formatVal2(count);// 格式化金额
		$("#" +destInputId).val(count);
	} 
	
	/**
	* 增加一行
	* @param tableId表格Id
	* @param radioId
	* @param nNewRowPosition
	* @param startRowIndex
	*/
	function addNewRowDeal(table){
		if (table.attr("id") == "tblOrgOrgs")
		{
			var maxLinNum=table.find("tr").length-4;//获得行数，减去表头和隐藏行
			var lineNum=""+(maxLinNum+2);
			var addHtml=table.find("tr")[2].innerHTML;//获得隐藏行的html,查找第二行
			addHtml=addHtml.replace(/\[index\]/g,lineNum);
			lineNum = (parseInt(lineNum, 10)-1).toString();//2011.12.13 行下标比行号小1(隐藏行下标[0]，第一行下标[00])
			if (lineNum.length==1)//如果是个位数，前面补0
			lineNum="0"+lineNum;
			addHtml=addHtml.replace(/\[0\]/g,"["+lineNum+"]");//替换html中行号
			addHtml=addHtml.replace(/\|/g,"/");//替换html中节点/的代替符"|"
			table.append("<tr>"+addHtml+"</tr>");//将html增加到最后
				
		}
		else
		{
			var maxLinNum=table.find("tr").length-2;;//获得行数，减去表头和隐藏行
			var lineNum=""+(maxLinNum+1);
			var addHtml=table.find("tr")[1].innerHTML;//获得隐藏行的html,查找第二行
			addHtml=addHtml.replace(/\[index\]/g,lineNum);
			lineNum = (parseInt(lineNum, 10)-1).toString();//2011.12.13 行下标比行号小1(隐藏行下标[0]，第一行下标[00])
			if (lineNum.length==1)//如果是个位数，前面补0
			lineNum="0"+lineNum;
			addHtml=addHtml.replace(/\[0\]/g,"["+lineNum+"]");//替换html中行号
			addHtml=addHtml.replace(/\|/g,"/");//替换html中节点/的代替符"|"
			table.append("<tr>"+addHtml+"</tr>");//将html增加到最后
		} 
	}
	
	/**
	* 上移行
	* @param tblId
	* @returns {Boolean}
	*/
 	function movePrev(tblId,selName) {
		if (tblId == "tblOrgOrgs")
		{		
			if (!selName)
				selName = "";
			if ($("#" + tblId + " :radio[name*='" + selName + "']:checked").length == 0) {
				scmWarn(irisNotes.prevWarn);
				return false;
			}
			if ($("#" + tblId + " tr").length <= 4)
				return false;
			var trChecked = $("#" + tblId + " :radio[name*='" + selName + "']:checked").parents("tr").first();// 取得被选中的行
			var trPrev = trChecked.prev(); // 取得上一行
			var index = trChecked.get(0).rowIndex; // 获得行号
			if (index <= 4) {// 已到第一行  不能上移
				scmWarn("协作单位"+irisNotes.tabelMoveWarn1);
				return false;
			}
			//trChecked.remove();
			trPrev.before(trChecked);
			trChecked.find(":radio[name*='" + selName + "']").attr("checked", 'true');
			refreshRowINdex(tblId);
		}
		else
		{
			 if (!selName)
				 selName = "";
				 if ($("#" + tblId + " :radio[name*='" + selName + "']:checked").length == 0) {
				 scmWarn(irisNotes.prevWarn);
				 return false;
				 }
				 if ($("#" + tblId + " tr").length <= 2)
				 return false;
				 var trChecked = $("#" + tblId + " :radio[name*='" + selName + "']:checked")
				 .parents("tr").first();// 取得被选中的行
				 var trPrev = trChecked.prev();// 取得上一行
				 var index = trChecked.get(0).rowIndex;// 获得行号
				 if (index <= 2) {// 如果是第一行
				 scmWarn(irisNotes.tabelMoveWarn1);
				 return false;
				 }
				 //trChecked.remove();
				 trPrev.before(trChecked);
				 trChecked.find(":radio[name*='" + selName + "']").attr("checked", 'true');
				 refreshRowINdex(tblId); 
		}
	}	 
	
	/**
	* 下移行
	* @param tblId
	* @returns {Boolean}
	*/
	 function moveNext(tblId,selName) {
		if (tblId == "tblOrgOrgs")
		{
			if (!selName)
				selName = "";
			if ($("#" + tblId + " :radio[name*='" + selName + "']:checked").length == 0) {
				scmWarn(irisNotes.nextWarn);
				return false;
			}
			if ($("#" + tblId + " tr").length <= 4) {
				return false;
			}
			var trChecked = $("#" + tblId + " :radio[name*='" + selName + "']:checked").parents("tr").first();// 取得被选中的行
			var trNext = trChecked.next();
			var index = trChecked.get(0).rowIndex;// 获得行号
			if (index == $("#" + tblId + " tr").length - 1) {// 如果是第一行
				scmWarn(irisNotes.tabelMoveWarn2);
				return false;
			}
			//trChecked.remove();
			trNext.after(trChecked);
			trChecked.find(":radio[name*='" + selName + "']").attr("checked", "true");
			refreshRowINdex(tblId);
		}
		else
		{
			 if (!selName)
				 selName = "";
				 if ($("#" + tblId + " :radio[name*='" + selName + "']:checked").length == 0) {
				 scmWarn(irisNotes.nextWarn);
				 return false;
				 }
				 if ($("#" + tblId + " tr").length <= 2) {
				 return false;
				 }
				 var trChecked = $("#" + tblId + " :radio[name*='" + selName + "']:checked")
				 .parents("tr").first();// 取得被选中的行
				 var trNext = trChecked.next();
				 var index = trChecked.get(0).rowIndex;// 获得行号
				 if (index == $("#" + tblId + " tr").length - 1) {// 如果是第一行
				 scmWarn(irisNotes.tabelMoveWarn2);
				 return false;
				 }
				 //trChecked.remove();
				 trNext.after(trChecked);
				 trChecked.find(":radio[name*='" + selName + "']").attr("checked", "true");
				 refreshRowINdex(tblId); 
		}
	}  
	
	
	
	/**
	* 重新计算行号
	* @param tblId
	*/
	
		function refreshRowINdex(tblId) {
			if (tblId == "tblOrgOrgs")
			{
				//重新计算行号
				$("#" + tblId + " tr").each(
						function() {
							var seq = $(this).get(0).rowIndex;//获得行数
							if (seq > 3) {//如果有行 显示时display行不计入类，故只减1
								seq = seq - 2;
								var seq_no = $(this).find("[name$='seq_no']");
								if (seq_no.is("span"))
									seq_no.html(seq);
								else
									seq_no.val(seq);
								
								seq = "" + (seq - 1);
								if (seq.length == 1)
									seq = "0" + seq;
								//替换相应的name参数的seq
								$(this).find("input,span,textarea,select").each(
										function() {
											var name = $(this).attr("name");
											if (name != null && name != "") {
												name = name.replace(/\[\d*\]/g, "["
														+ seq + "]");
												$(this).attr("name", name);
											}
											var id = $(this).attr("id");
											if (id != null && id != "") {
												id = id.replace(/\[\d*\]/g, "["
														+ seq + "]");
												$(this).attr("id", id);
											}
											var className = $(this).attr("class");
											if (className != null
													&& className != "") {
												className = className
														.replace(/\[\d*\]/g, "["
																+ seq + "]");
												$(this).attr("class", className);
											}
										});
							}
						});
			}
			else
			{
				$("#"+tblId+" tr").each(function(){
					var seq=$(this).get(0).rowIndex;//获得行数
					if(seq>1){//如果有行 显示时display行不计入类，故只减1
					seq=seq-1;
					var seq_no=$(this).find("[name$='seq_no']");
					if(seq_no.is("span"))
					seq_no.html(seq);
					else
					seq_no.val(seq);
					seq=""+(seq-1);
					if(seq.length==1)
					seq="0"+seq;
					//替换相应的name参数的seq
					$(this).find("input,span,textarea,select").each(function(){
					var name=$(this).attr("name");
					if(name!=null&&name!=""){
					name=name.replace(/\[\d*\]/g,"["+seq+"]");
					$(this).attr("name",name);
					}
					var id=$(this).attr("id");
					if(id!=null&&id!=""){
					id=id.replace(/\[\d*\]/g,"["+seq+"]");
					$(this).attr("id",id);
					}
					var className=$(this).attr("class");
					if(className!=null&&className!=""){
					className=className.replace(/\[\d*\]/g,"["+seq+"]");
					$(this).attr("class",className);
					}
					});
					}
					}
					); 
			}
		}
	</script>
	
